/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useState } from 'react';
import { Button, Statistic, message } from 'antd';

const { Countdown } = Statistic;

const Index = ({
  onCheck,
  sendCode,
  queryData,
}: {
  onCheck: Function;
  sendCode: Function;
  queryData: any;
}) => {
  const [countdownStatus, setCountdownStatus] = useState<boolean>(false);
  const getVerificCode = () => {
    onCheck().then(async (res: any) => {
      if (res.phone) {
        const { code, msg } = await sendCode(queryData);
        if (code == 0) {
          message.success('获取验证码成功');
          setCountdownStatus(true);
        } else {
          message.error(msg);
        }
      }
    });
  };
  return (
    <Button disabled={countdownStatus} size='small' onClick={getVerificCode} type='link'>
      {countdownStatus ? (
        <Countdown
          value={Date.now() + 1000 * 60}
          format='ss'
          onFinish={() => setCountdownStatus(false)}
          suffix='后重新获取'
          valueStyle={{ fontSize: '12px', color: '#666' }}
        />
      ) : (
        '获取验证码'
      )}
    </Button>
  );
};

export default Index;
